package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.ActivityMyBean;
import com.cljz.makepassenger.ui.holder.MineActivityHolder;

import java.util.List;

/**
 * Created by apple on 2017/5/2.
 */

public class MineActivityAdapter extends RecyclerView.Adapter<MineActivityHolder> {
    private Context context;
    private List<ActivityMyBean.DataBean> activityMyList;
    private OnItemClickListener mOnItemClickListener;

    public MineActivityAdapter(Context context, List<ActivityMyBean.DataBean> activityMyList) {
        this.context = context;
        this.activityMyList = activityMyList;
    }

    @Override
    public MineActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MineActivityHolder(View.inflate(context, R.layout.item_inviter_list, null));
    }

    @Override
    public void onBindViewHolder(MineActivityHolder holder, final int position) {
        holder.setData(activityMyList, position);
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(activityMyList.get(position));
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(activityMyList.get(position));
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return activityMyList == null ? 0 : activityMyList.size();
    }

    public void setData(List<ActivityMyBean.DataBean> data) {
        this.activityMyList = data;
        notifyDataSetChanged();
    }

    public void addData(List<ActivityMyBean.DataBean> data) {
        int startPosition = this.activityMyList.size() + 1;
        this.activityMyList.addAll(data);
        notifyItemRangeInserted(startPosition,data.size());
    }

    public interface OnItemClickListener {
        void onClick(ActivityMyBean.DataBean dataBean);

        void onLongClick(ActivityMyBean.DataBean dataBean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

}
